/**
 * Created by jiachenpan on 16/11/18.
 */


export function formatTime(dateTimeStamp) {
  var minute = 1000 * 60;
  var hour = minute * 60;
  var day = hour * 24;
  var halfamonth = day * 15;
  var month = day * 30;
  var now = new Date().getTime();
  var diffValue = now - dateTimeStamp;
  var result;
  if (diffValue < 0) { return; }
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  if (monthC >= 1) {
    result = "" + parseInt(monthC) + "月前";
  }
  else if (weekC >= 1) {
    result = "" + parseInt(weekC) + "周前";
  }
  else if (dayC >= 1) {
    result = "" + parseInt(dayC) + "天前";
  }
  else if (hourC >= 1) {
    result = "" + parseInt(hourC) + "小时前";
  }
  else if (minC >= 1) {
    result = "" + parseInt(minC) + "分钟前";
  } else
    result = "刚刚";
  return result;
}

export function formatString(value) {
  if (value == null || value == "" || value == undefined) {
    return "";
  } else {
    var str = value.replace(/\ +/g, "");
    str = str.replace(/[\r\n]/g, "");
    return str;
  }

}
export function isBase64Encode(str) {
  str = formatString(str)
  var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/
  if (base64regex.test(str)) {
    return true
  } else {
    return false
  }
}

export function guid(split = '') {
  let guid = ''
  for (let i = 1; i <= 32; i++) {
    const n = Math.floor(Math.random() * 16.0).toString(16)
    guid += n
    if ((i === 8) || (i === 12) || (i === 16) || (i === 20)) {
      guid += split
    }
  }
  return guid
}

/**
 *
 * @param {String} ctx 为要复制的内容
 * @param {Function} cb 操作回调
 */
export function doCopy(cxt, cb) {
  var doms = document.querySelectorAll('.copy_0831');
  [].forEach.call(doms, x => {
    document.body.removeChild(x)
  })
  var dom = document.createElement('div')
  dom.style = '-webkit-user-select: text;user-select: text;'
  dom.innerHTML = cxt
  document.body.appendChild(dom)
  var copyDOM = dom // 要复制文字的节点
  var range = document.createRange()
  // 选中需要复制的节点
  range.selectNode(copyDOM)
  // 执行选中元素
  window.getSelection().addRange(range)
  // 执行 copy 操作
  var successful = document.execCommand('Copy')
  try {
    if (successful) {
      // eslint-disable-next-line standard/no-callback-literal
      cb && cb(true)
    } else {
      // eslint-disable-next-line standard/no-callback-literal
      cb && cb(false)
      console.log('复制失败')
    }
  } catch (err) {
    // eslint-disable-next-line standard/no-callback-literal
    cb && cb(false)
    console.log('复制失败')
  }
  dom.className = 'copy_0831'
  dom.style.display = 'none'
  // 移除选中的元素
  window.getSelection().removeAllRanges()
}

import calendar from './lib/calendar'

import {
  setStorage, setStorageJSON, getStorage, getStorageJSON, setStorageExpires, getStorageExpires
} from './storage'

import {
  getRandName,
  getRandBankCard,
  getRandMobile
} from './string'

import {
  getAddress,
  getAreaInfo,
  getCardIdInfo,
  getRandCardId
} from './cardId'

export {
  calendar,
  setStorage,
  setStorageJSON,
  getStorage,
  getStorageJSON,
  setStorageExpires,
  getStorageExpires,
  getRandName,
  getRandBankCard,
  getRandMobile,
  getAddress,
  getAreaInfo,
  getCardIdInfo,
  getRandCardId
}
